const app = getApp()
const date = new Date()
const years = []
const year = date.getFullYear()
const months = []
const month = String(date.getMonth() + 1).padStart(2, '0');
const days = []
const day = String(date.getDate()).padStart(2, '0')
for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}
for (let i = 1; i <= 12; i++) {
  months.push(i)
}
for (let i = 1; i <= 31; i++) {
  days.push(i)
}
export default {
  props: {
    defaultValue: {
      type: String,
      default: `${year}-${month}-${day}`
    }
  },
  data: function () {
    return {
      title: 'picker-view',
      years,
      year,
      months,
      month,
      days,
      day,
      value: [],
      visible: true,
      indicatorStyle: {'line-height': '34px'}
    }
  },
  watch: {
    defaultValue: {
      deep: true,
      immediate: true,
      handler(newValue) {
        const values = newValue.split('-')
        if (newValue) {
          this.value = [values[0], values[1] - 1, values[2] - 1]
        }
      }
    }
  },
  methods: {
    togglePopup(isShow) {
      this.$refs.basicPopup.togglePopup(isShow)
    },
    handleConfirm() {
      this.$emit('onConfirm', `${this.year}-${this.month}-${this.day}`)
      this.togglePopup(false);
    },
    handleCancel() {
      this.togglePopup(false);
    },
    bindChange(e) {
      const val = e.detail.value
      this.year = this.years[val[0]];
      this.month = String(this.months[val[1]]).padStart(2, '0');
      this.day = String(this.days[val[2]]).padStart(2, '0');
    }
  }
}